package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Catalog;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.CatalogService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/catalog/*")
public class CatalogServlet extends BaseServlet {
    private CatalogService catalogService = BeanFactory.getBean("catalogService",CatalogService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            
            List<Catalog> list = catalogService.findAll();
            return new Result(true,MessageConstant.QUERY_CATALOG_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CATALOG_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            
            catalogService.delete(ids);
            return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Catalog catalog = BeanUtil.fillBeanFromJson(request, Catalog.class);
            
            catalogService.update(catalog);
            return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String id = request.getParameter("id");
            
            Catalog catalog = catalogService.findById(id);
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,catalog);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Catalog catalog = BeanUtil.fillBeanFromJson(request, Catalog.class);
            
            catalogService.save(catalog);
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }
    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageStr)){
                currentPage=Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)){
                pageSize=Integer.parseInt(pageSizeStr);
            }
            
            PageInfo<Catalog> info =catalogService.findByPage(currentPage,pageSize);
            return new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMPANY_FAIL);
        }

    }

    
}
